namespace TLang.Values.Primitives
{
    using Ast;
    using System.Collections.Generic;

    public class Mult : PrimFun
    {

        public Mult() : base("*", 2)
        {
            
        }

        public override Value Apply(List<Value> args, Node location)
        {
            Value v1 = args[0];
            Value v2 = args[1];
            if (v1 is IntValue && v2 is IntValue)
            {
                return new IntValue(((IntValue)v1).Value * ((IntValue)v2).Value);
            }
            if (v1 is FloatValue && v2 is FloatValue)
            {
                return new FloatValue(((FloatValue)v1).Value * ((FloatValue)v2).Value);
            }
            if (v1 is FloatValue && v2 is IntValue)
            {
                return new FloatValue(((FloatValue)v1).Value * ((IntValue)v2).Value);
            }
            if (v1 is IntValue && v2 is FloatValue)
            {
                return new FloatValue(((IntValue)v1).Value * ((FloatValue)v2).Value);
            }

            Util.Abort(location, "incorrect argument types for *: " + v1 + ", " + v2);
            return null;
        }


        public override Value Typecheck(List<Value> args, Node location)
        {
            Value v1 = args[0];
            Value v2 = args[1];

            if (v1 is FloatType || v2 is FloatType)
            {
                return new FloatType();
            }
            if (v1 is IntType && v2 is IntType)
            {
                return Type.INT;
            }
            Util.Abort(location, "incorrect argument types for *: " + v1 + ", " + v2);
            return null;
        }
    }
}